package com.hjm.mall.api;

import com.hjm.mall.api.param.OrderInsertParam;
import com.hjm.mall.api.param.OrderUpdateParam;
import com.hjm.mall.config.annotation.TokenToMallUserId;
import com.hjm.mall.entity.Order;
import com.hjm.mall.service.impl.OrderServiceImpl;
import com.hjm.mall.util.Result;
import com.hjm.mall.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@ResponseBody
@RequestMapping("/api")
@Api(value = "order",tags = "哈基米商城——订单接口")
public class OrderController {
    @Autowired
    OrderServiceImpl orderService;

    @GetMapping("/order")
    @ApiOperation(value = "根据id查找订单", notes = "需要token验证")
    public Result<Order> getOrderById(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId,
            @ApiParam("订单id")
            @RequestParam("id")
                    Integer id){

        if (id == null)
            return ResultGenerator.genErrorResult(404,"订单id不正确！");

        return ResultGenerator.genSuccessResult(orderService.getOrderById(id));
    }

    @PutMapping("/order")
    @ApiOperation(value="修改订单信息", notes="")
    public Result updateOrder(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId,
            @RequestBody
            @ApiParam("订单信息")
                    OrderUpdateParam orderUpdateParam){

        boolean res = orderService.update(orderUpdateParam);
        if (res){
            return ResultGenerator.genSuccessResult("更新成功");
        }
        return ResultGenerator.genFailResult("更新失败");
    }

    @PostMapping("/order")
    @ApiOperation(value = "新增订单", notes = "")
    public Result insertOrder(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId,
            @RequestBody
            @ApiParam("订单信息")
                    OrderInsertParam orderInsertParam){

        Integer id = orderService.insert(userId, orderInsertParam);
        if (id != null){
            return ResultGenerator.genSuccessResult(id);
        }
        return ResultGenerator.genFailResult("新增订单失败");
    }

    @DeleteMapping("/order")
    @ApiOperation(value = "删除订单", notes = "")
    public Result deleteOrder(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId,
            @ApiParam("订单id")
            @RequestParam("id")
                    Integer id){

        if (id == null)
            return ResultGenerator.genErrorResult(404,"订单id不正确！");

        boolean res = orderService.delete(id);
        if (res){
            return ResultGenerator.genSuccessResult("删除成功");
        }
        return ResultGenerator.genFailResult("删除订单失败");
    }

    @GetMapping("/order/list")
    @ApiOperation(value = "获取所有订单", notes = "")
    public Result getOrderList(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId){

        Result result = ResultGenerator.genSuccessResult(orderService.getOrderListByUserId(userId));
        return result;
    }

    @GetMapping("/order/sc")
    @ApiOperation(value = "获取购物车VO", notes = "")
    public Result getShopCartVO(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId){

        return ResultGenerator.genSuccessResult(orderService.getShopCartVOList(userId));
    }

    @PostMapping("/order/co")
    @ApiOperation(value = "创建订单", notes = "")
    public Result createOrder(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
            Long userId,
            @RequestBody
            int[] orderIds){

        return ResultGenerator.genSuccessResult(orderService.getOrderCreateVOList(userId,orderIds));
    }
}
